package com.lee.controller.system;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 *  字典数据管理
 * @author LiPengFei
 *
 */
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lee.aspect.Log;
import com.lee.base.BaseController;
import com.lee.base.Result;
import com.lee.constant.BusinessType;
import com.lee.page.PageResult;
import com.lee.page.TableSupport;
import com.lee.system.api.IDictDataService;
import com.lee.system.entity.DictData;

import io.swagger.annotations.ApiOperation;
@Controller
@RequestMapping("/system/dict/data")
public class DictDataController extends BaseController{
	
	private String prefix="system/dict/data";
	@Reference
	private IDictDataService dictDataService;
	@RequiresPermissions("system:dict:view")
	@GetMapping()
	public String dictData(){
		return prefix+"data";
	}
	@ApiOperation("查询字典数据列表")
	@RequiresPermissions("system:dict:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(DictData dictData){
		PageResult pageResult = dictDataService.selectDictDataList(dictData,TableSupport.getPageDomain());
		return Result.ok().page(pageResult.getRows());
	}
	/**
	 * 新增字典数据
	 * @param dictType
	 * @param map
	 * @return
	 */
	@Log(title="字典数据管理",action = BusinessType.INSERT)
	@RequiresPermissions("system:dict:add")
	@GetMapping("/add/{dictType}")
	public String add(@PathVariable("dictType") String dictType,Map<String, Object> map){
		map.put("dictType", dictType);
		return prefix+"/add";
	}
	/**
	 * 修改字典数据
	 * @param dictCode
	 * @param map
	 * @return
	 */
	@Log(title="字典数据管理",action = BusinessType.UPDATE)
	@RequiresPermissions("system:dict:edit")
	@GetMapping("/edit/{dictCode}")
	public String edit(@PathVariable("dictCode")Long dictCode,Map<String, Object> map){
		map.put("dict", dictDataService.selectDictDataById(dictCode));
		return prefix+"/edit";
	}
	/**
	 * 保存字典数据
	 * @param dictData
	 * @return
	 */
	@Log(title="字典数据管理",action=BusinessType.SAVE)
	@ApiOperation("保存字段数据")
	@RequiresPermissions("system:dict:save")
	@PostMapping("/save")
	@ResponseBody
	public Result save(DictData dictData){
		if(dictDataService.saveDictData(dictData)>0){
			return Result.ok();
		}
		return Result.error();
	}
	/**
	 * 删除字典数据
	 * @param ids
	 * @return
	 */
	@Log(title="字典数据管理",action=BusinessType.DELETE)
	@ApiOperation("删除字典数据")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		
		if(dictDataService.deleteDictDataByIds(ids)>0){
			return Result.ok();
		}
		return Result.error();
		
	}
	
}
